use std::time::Duration;

use dbus_tokio::connection;

use super::super::nonblock;
use super::super::Result;

#[tokio::test]
async fn test_nonblock() -> Result<()> {
    let (res, conn) = connection::new_system_sync()?;
    tokio::spawn(async {
        let err = res.await;
        panic!("Lost connection to D-Bus: {}", err);
    });

    let timeout = Duration::from_secs(3);

    let object_manager = nonblock::OrgBluezObjectManager::new(conn, timeout);

    // Adapters
    {
        use super::super::nonblock::OrgBluezAdapter1;
        println!("Adapters");
        let adapters = object_manager.list_adapters().await?;
        for adapter in adapters {
            let name = adapter.name().await?;
            let address = adapter.address().await?;
            println!("{} - {}", address, name);
        }
    }

    // Devices
    {
        use super::super::nonblock::OrgBluezDevice1;
        println!("Devices");
        let devices = object_manager.list_devices().await?;
        for dev in devices {
            let name = dev.name().await?;
            let address = dev.address().await?;
            println!("{} - {}", address, name);
        }
    }

    Ok(())
}
